import pandas as pd
import os,random

#预处理持仓数据，增加证券分类
positionFile="C:\\Ax\安信OneQuant\\Data\\positionInfo_20230306.csv"
positionAll=pd.read_csv(positionFile)
positionAll["证券分类"]=positionAll["securityID"].str[:2].apply(lambda x:"深市主板" if x=="00"
                                        else "深市可转债" if x=="11"
                                      else "深市创业板" if x=="30"
                                        else "深市ETF基金" if x=="15"
                                      else "沪市主板" if x=="60"
                                        else "沪市可转债" if x=="12"
                                      else "沪市ETF基金" if x=="51" else "沪市ETF基金" if x=="56"  else "沪市ETF基金" if x=="58"
                                        else "沪市科创板" if x=="68"
                                       else '--')


# pyFilePath=os.path.split(os.path.realpath(__file__))[0]

# 菜单配置文件，根据不同账号分配不同菜单
demoDric="C:\\Users\\AXZQ\\Desktop\\AUTO\\OQ_AUTOTest"
# accountDistri={"算法交易":"880100001139","DMA算法":"880100001139","算法单导入":"888821600235","网格交易":"880100001139","T0算法":"888821600235","调仓算法":"880100001139",}
testconfig=pd.read_excel(os.path.join(demoDric,"testConfig.xls"))
my_meau_list=testconfig['菜单'].to_list()

for i in my_meau_list:
    testAccount=my_meau_list[my_meau_list['菜单']==i]["账号"].iloc[0]
    testAccountType=my_meau_list[my_meau_list['菜单']==i]["账号类型"].iloc[0]
    testNum=my_meau_list[my_meau_list['菜单']==i]["标的数量"].iloc[0]
    if i=="DMA算法":
        demoListNew=pd.DataFrame(columns=['updTime', 'custBatchNo', 'acctType', 'acct', 'symbol', 'tradeSide', 'targetQty', 'algoId', 'priceType', 'ticks', 'highLimitPrice', 'lowLimitPrice'])
        for filename in os.listdir(demoDric):
            if testAccount in filename :
                full_path = os.path.join(demoDric, filename)
                positionData=pd.read_excel(full_path)
                positionData=positionData.sort_values(by='可用数量', ascending=False).head(500)
                symbolList=random.shuffle(positionData['代码'].to_list())
                batchNo=random.randint(1, testNum*20)
                symbolLimit=0
                for i in  symbolList:
                    batchNo=batchNo+1
                    symbolLimit=symbolLimit+1
                    if testAccountType in ["UM0","UFO","AT0"]:
                        # 针对股票账户
                        if symbolLimit<=int(testNum/2):
                            # 输出卖出指令
                            DMAList=["144550.123",str(batchNo),testAccountType,testAccount,i,"2",str(min(5000,int(positionData[positionData["代码"]=i]["可用数量"]/20))),"DMA","P8","0","0","0"]
                            demoList=pd.DataFrame(DMAList)
                            demoListNew=pd.concat([demoListNew,demoList])
                        elif  symbolLimit>int(testNum/2) and symbolLimit<=testNum:
                            DMAList=["144550.123",str(batchNo),testAccountType,testAccount,i,"1",str(min(5000,int(positionData[positionData["代码"]=i]["可用数量"]/20))),"DMA","P8","0","0","0"]
                            demoList=pd.DataFrame(DMAList)
                            demoListNew=pd.concat([demoListNew,demoList])
                        elif symbolLimit>testNum:
                            break
                    # 针对信用账户
                    elif symbolLimit<=int(testNum/4):
                        # 输出担保品卖出指令
                        DMAList=["144550.123",str(batchNo),testAccountType,testAccount,i,"2",str(min(5000,int(positionData[positionData["代码"]=i]["可用数量"]/20))),"DMA","P8","0","0","0"]
                        demoList=pd.DataFrame(DMAList)
                        demoListNew=pd.concat([demoListNew,demoList])
                    elif  symbolLimit>int(testNum/4) and symbolLimit<=int(testNum/2):
                        # 输出卖券还款指令
                        DMAList=["144550.123",str(batchNo),testAccountType,testAccount,i,"D",str(min(5000,int(positionData[positionData["代码"]=i]["可用数量"]/20))),"DMA","P8","0","0","0"]
                        demoList=pd.DataFrame(DMAList)
                        demoListNew=pd.concat([demoListNew,demoList])
                    elif  symbolLimit>int(testNum/2) and symbolLimit<=int(3*testNum/4):
                        # 输出融资买入指令
                        DMAList=["144550.123",str(batchNo),testAccountType,testAccount,i,"A",str(min(5000,int(positionData[positionData["代码"]=i]["可用数量"]/20))),"DMA","P8","0","0","0"]
                        demoList=pd.DataFrame(DMAList)
                        demoListNew=pd.concat([demoListNew,demoList])
                    elif  symbolLimit>int(3*testNum/4) and symbolLimit<=testNum:
                        # 输出担保品买入指令
                        DMAList=["144550.123",str(batchNo),testAccountType,testAccount,i,"1",str(min(5000,int(positionData[positionData["代码"]=i]["可用数量"]/20))),"DMA","P8","0","0","0"]
                        demoList=pd.DataFrame(DMAList)
                        demoListNew=pd.concat([demoListNew,demoList])
                        elif symbolLimit>testNum:
                            break







